package com.project.service.impl;

import com.project.bean.UserBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IGradeMapper gradeMapper;

    @Override
    public void add(UserBean user, Integer[] gradeIdArray) {
        userMapper.insert(user);
        userMapper.addGradeList(user.getId(),gradeIdArray);
    }

    @Override
    public void del(Integer id) {
        userMapper.delUserGrade(id);
        userMapper.deleteById(id);
    }

    @Override
    public void update(Integer id, Integer[] newGradeArray) {
        userMapper.delUserGrade(id);
        userMapper.addGradeList(id,newGradeArray);
    }

    @Override
    public UserBean findById(Integer id) {
        UserBean user = userMapper.selectById(id);
        user.setGradeList(gradeMapper.findByUser(id));
        return user;
    }
}
